package l1j.opqlo.NewSystem.ItemStatus;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.server.DatabaseFactory;
import l1j.server.server.utils.SQLUtil;

/** opqlo 新增物品自訂描述 */
public class ItemStatusTable {

	private static Logger _log = Logger.getLogger(ItemStatusTable.class
			.getName());

	private static ItemStatusTable _instance;

	public static ItemStatusTable getInstance() {
		if (_instance == null) {
			_instance = new ItemStatusTable();
		}
		return _instance;
	}

	private final HashMap<Integer, ItemStatusTable> _itemIdIndex = new HashMap<Integer, ItemStatusTable>();

	@SuppressWarnings("unused")
	private int _itemId;

	private String _msg;

	private ItemStatusTable() {
		this.loadItemStatus();
	}

	private ItemStatusTable(final int id, final String msg) {
		this._itemId = id;
		this._msg = msg;
	}

	private void fillItemStatus(final ResultSet rs) throws SQLException {
		while (rs.next()) {

			final int ItemID = rs.getInt("ItemID");
			if (ItemID <= 0) {
				continue;
			}

			final String status = rs.getString("解說");

			final ItemStatusTable itemstatus = new ItemStatusTable(ItemID,
					status);

			this._itemIdIndex.put(ItemID, itemstatus);
		}
	}

	public final String getItemStatusMsg(final int itemid) {
		final ItemStatusTable check = ItemStatusTable.getInstance()
				.getTemplate(itemid);
		if (check != null) {
			return check._msg;
		} else {
			return null;
		}
	}

	public ItemStatusTable getTemplate(final int itemId) {
		return this._itemIdIndex.get(itemId);
	}

	private void loadItemStatus() {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {

			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("SELECT * FROM Z_opqlo_ItemStatus");
			rs = pstm.executeQuery();
			this.fillItemStatus(rs);
		} catch (final SQLException e) {
			_log.log(Level.SEVERE,
					"error while creating Z_opqlo_ItemStatus table", e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}
}
